package com.rest.demo.nio.socketdemo;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * @author Administrator
 * @decription
 * @Date 2018-02-09 10:13
 */
public class SocketServer {

    public static void main(String[] args) throws IOException {
        try {
            ServerSocketChannel ssc = ServerSocketChannel.open();
            ssc.socket().bind(new InetSocketAddress("116.196.84.9",8001));
            SocketChannel socketChannel = ssc.accept();

            ByteBuffer buffer = ByteBuffer.allocate(256);
            socketChannel.read(buffer);
            buffer.flip();
            StringBuffer sb = new StringBuffer();
            while(buffer.hasRemaining()) {
                sb.append((char)buffer.get());
            }
            System.out.println(sb.toString());
            socketChannel.close();
            ssc.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
